/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp'); 
	app.controller('WarningApproveListCtrl', ['$scope','FwaTabsetService','WarningAddService','$modal','BaseURL','Modal','WarningService',
	                                function($scope,FwaTabsetService,WarningAddService,$modal,baseUrl,Modal,WarningService){
		$scope.warningService = WarningService;
		WarningService.initMedicalOrgInfoList();
		$scope.form = {};
		$scope.form.warnDate = {};
//		$scope.warningFrom={};//预警信息数组
//		$scope.warnStaffData=[];//医保人数据
//		$scope.warnStaffCount=0;//设置医保人数为0
//		$scope.warnInsurData=[];//参保人数据
//		$scope.warnInsurCount=0;//设置参保人数为0
//		$scope.warnScreenList=[];//网上筛选数据
//		$scope.notice = {};//文档内容数组
//		$scope.warnMedicalData={};//机构数组
//		$scope.warnMedicalCount=0;
		var currentPage = 1;
		var cellTemplate = 
			'<td>'+
				'<a  ng-click="clickLink(\'edit\',data)"">{{data.tranName}}</a>'+
			'</td>',
			cellTemplateView = 
				'<td >'+
				   '<a title=\'{{data.warnNoBiz}}\' ng-click="clickLink(\'warnQueryApprove.warningView\',data)"">{{data.warnNoBiz}}</a>'+
			    '</td>',
			indexNo = '<td>'+
				'{{$index+1}}'+
			'</td>';
       $scope.columns = [
        {label:'预警编号',width:'8%',cellClass:'text-left', cellTemplate:cellTemplateView},
        {label:'预警事件名称',map:'summary',width:'10%'},
        {label:'机构名称',map:'medicalOrgName',width:'12%'},
        {label:'处理方式',map:'process',width:'8%'},
        {label:'预警时段',map:'happenDate',width:'14%'},
        {label:'反馈时间',map:'feedbackDate',width:'10%'},
        {label:'受理时间',map:'warnDate',width:'10%'},
        {label:'审核时间',map:'auditDate',width:'10%'},
        {label:'事件状态',map:'actName',width:'10%'},
        {label:'操作',width:'8%',cellClass:'text-left', cellTemplate:cellTemplate}

    ];
       $scope.clickLink = function (url,data){
    	   if(url==='edit'){
    		   FwaTabsetService.selectTab('warnQueryApprove.warningApproveWarn',{warnNo:data.warnNo,urlFlag:''},false);
    	   }else{
    		   FwaTabsetService.selectTab(url,{warnNo:data.warnNo,actNo:'approval',isBack:'N'},false);
    	   }
       }

		//每页显示数
        var start = 0,
            limit = 20;
        
//        $scope.form = {
//            page:"0",
//            pageSize:"20"
//        };

        
        var params = function (){
			var warnNoBiz=null;
			if($scope.form.warnNoBiz&&!angular.isUndefined($scope.form.warnNoBiz)){
				warnNoBiz = $scope.form.warnNoBiz;
			}
			var medicalOrgCode=null;
			if($scope.form.medicalOrgCode&&!angular.isUndefined($scope.form.medicalOrgCode)){
				medicalOrgCode = $scope.form.medicalOrgCode;
			}
			var summary=null;
			if($scope.form.summary&&!angular.isUndefined($scope.form.summary)){
				summary = $scope.form.summary;
			}
			var feedbackDate=null;
			if($scope.form.feedbackDate&&!angular.isUndefined($scope.form.feedbackDate)){
				feedbackDate = $scope.form.feedbackDate;
			}
//			var actNo=null;
//			if($scope.form.actNo&&!angular.isUndefined($scope.form.actNo)){
//				actNo = $scope.form.actNo;
//			}
			var startDate=null;
			if($scope.form.warnDate.start&&!angular.isUndefined($scope.form.warnDate.start)){
				startDate = $scope.form.warnDate.start;
			}
			var endDate=null;
			if($scope.form.warnDate.end&&!angular.isUndefined($scope.form.warnDate.end)){
				endDate = $scope.form.warnDate.end;
			}
			
			if(parseInt(currentPage) > 1){
                  start = (parseInt(currentPage) - 1) * limit;
            }
			var params = {
					warnNoBiz:warnNoBiz,
					medicalOrgCode:medicalOrgCode,
					summary:summary,
					feedbackDate:feedbackDate,
//					actNo:actNo,
					startDate:startDate,
					endDate:endDate,
					start:start,
					limit:limit
			}
			return params;
		}
        
        //查询
        function query(){
        	WarningService.warningApproveQuery(params()).success(function(res){
                $scope.tableData = res.result;
                $scope.totalCount = res.total;
            });
        }

		//分页查询
        $scope.$on('pageTool.skipPage',function(e,v){
            start = limit*(v-1);
            $scope.form.page = start;
            //$scope.onSubmit();
            query();
        });

        //页面点击查询
        $scope.onSubmit = function(){
        	start=0;
        	$scope.currentPage=1;
        	query();
        };
		
        //加载数据
        $scope.onSubmit();
        
         //重置
        $scope.resetBut = function(){
			$scope.form = {};
			$scope.form.warnDate = {};
		}
        
        //-----------------------------------------     
       /* //循环将选中的列装入集合中
        $scope.approveList=null;
        function queryBatchApprove(){
        	var approveList=[];
        	angular.forEach($scope.tableData,function(data){
	        	if(data.checked){
	        		approveList.push(data);
	        	}
        	});
        	if(approveList.length>0){
        		$scope.approveList=approveList;
        	}
        
        }*/
        
      //审批意见
		$scope.codetypes=[]; 
		var approveCodeType=['warn_approve'];
		function selectAuditResultDate(){
			WarningAddService.getHcpmsCodeList(approveCodeType).success(function(response){
				$scope.codetypes = response.result;
			});
		}
		selectAuditResultDate()//调用审批意见的方法
		
        //弹框，批量处理
        $scope.waringshs=function(){
        	//queryBatchApprove();
			 //循环将选中的列装入集合中
	        $scope.approveList=null;	        
	        	var approveList=[];
	        	angular.forEach($scope.tableData,function(data){
		        	if(data.checked){
		        		approveList.push(data);
		        	}
	        	});
	        	if(approveList.length>0){
	        		$scope.approveList=approveList;
	        	}        
	       
        	if(!$scope.approveList){
        		Modal.alert("请勾选审批项");
        	}else{
        		var scope=$scope.$new();;
        		scope.codetypes=$scope.codetypes;
        			var modalInstance = $modal.open({
        				templateUrl:'resultAdd_view.html',
      			      	backdrop:'static',
      			      	size:'sm',
      			        scope:scope,
      				  controller: function($scope, $modalInstance,Modal,CommunityService){
    					  $scope.communityService = CommunityService;
    					  CommunityService.initCodeListDatas();	//获取所有的数据字典数据				  
    					  $scope.form = {};
    					  
    					  //关闭弹窗
    					  $scope.onOk = function() {
    						  $modalInstance.dismiss();
    					  }
    					  
    					  //确定事件
    					  $scope.confirm=function(){
//    						  Modal.alert($scope.form.deptCode);
//    						  Modal.alert($scope.form.warnfeeTypeCode);
    						  if(!$scope.form.deptCode){
      							Modal.alert("请选择审核意见!!!");
      							return false;
      						  }    						  
    						  if($scope.form.deptCode==='02'&&!$scope.form.warnfeeTypeCode){
      							Modal.alert("意见说明不能为空!!!");
      							return false; 
      						  }
    						 //封装传到后台的参数
    						  var params={
        							  approveList:$scope.approveList,//批量审批集合
        							  deptCode:$scope.form.deptCode,//审核意见
        							  warnfeeTypeCode:$scope.form.warnfeeTypeCode//意见说明
        			                 }; 
    						
    						  //数据的后台验证保存
    						  WarningAddService.approveWarnsps(params).success(function(data){
    							  if(params.deptCode === '02'){//如果审批意见为02
    					        	   params.isCheckBack="1";
    							  }
    					           else{
    					        	   params.isCheckBack="0";
    					           }
    							  Modal.alert(data.msg);
    							  if(data.state=='0') {
    								   WarningAddService.warningMarks = [];//初始化扣分参数
    								   //加载数据
    							        $scope.onSubmit();
    				               }
    						  });//end success()
    						  
    						  $modalInstance.close($scope.form);//关闭当前弹窗
    					  }//end confirm
    					 
    				  }//end controller
        			
        			});//end $modal.open()
        			
        		//});
        	}//end else
        	
        }
        //-----------------------------------------
      
	}]);
})();